package cz.via.wowss.helper;

import java.sql.Timestamp;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import cz.via.wowss.controller.MainController;
import cz.via.wowss.dao.CompanyDao;
import cz.via.wowss.entities.Company;
import cz.via.wowss.entities.CompanyHistory;

public class HistoryMaker implements Runnable {


	public void run() {
		while (true) {
			EntityManager em = MainController.emf.createEntityManager();
			EntityTransaction t = em.getTransaction();
			t.begin();
			for (Company c : new CompanyDao().getCompanies()) {
				CompanyHistory ch = new CompanyHistory(c, new Timestamp(new java.util.Date().getTime()), c.getValue());
				em.persist(ch);
			}
			t.commit();
			try {
				Thread.sleep(3600000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
